﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.uc
{
    public partial class Shop : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Shop), Page);
            var shop = Request.QueryString["shop"];
            if (shop == null || (!CommonBll.IsLegitimateShopId(shop)) || ClientInfoBll.GetClientById(CommonBll.GetTheRealId(shop).ToString(),  CommonConst.DB_Name).Rows.Count == 0)
            {
                Response.Redirect("/Shop/error.html");
            }
            hdshopId.Value = shop;
            hdtypeId.Value = Request.QueryString["product_type"] ?? "";
        }

        [AjaxPro.AjaxMethod]
        public string GetProductTypeList(string shop, string typeId)
        {
            var jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                var dt = ProductInfoBll.GetGoodsClassWhereHasItems(CommonBll.GetTheRealId(shop).ToString(), CommonConst.DB_Name);
                var builder = new StringBuilder();
                foreach (DataRow row in dt.Rows)
                {
                    if (typeId != row["GC_PId"].ToString())
                    {
                        builder.AppendFormat(
                            "<li><a href=\"/Shop{0}/product/{1}.html\">{2}</a></li>", shop, row["GC_PId"],
                            row["gcname"]);
                    }
                    else
                    {
                        builder.AppendFormat(
                            "<li class=\"bh-usx-yy\"><a href=\"/Shop{0}/product/{1}.html\">{2}</a></li>",
                            shop, row["GC_PId"], row["gcname"]);
                    }
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod]
        public string GetContact(string shop)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();

            if (CommonBll.IsLegitimateShopId(shop))
            {
                var dt = ClientInfoBll.GetClientById(CommonBll.GetTheRealId(shop).ToString(), CommonConst.DB_Name);
                if (dt.Rows.Count!=0)
                {
                    jsonResult.Add("isreload",true);
                    StringBuilder builder=new StringBuilder();
                    if (dt.Rows[0]["User_CellPhone"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>移动电话：</span> <span title=\"{0}\" class=\"text\"> {0}</span></li>", dt.Rows[0]["User_CellPhone"]);
                    }
                    if (dt.Rows[0]["User_TelPhone"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>固定电话：</span> <span title=\"{0}\"  class=\"text\"> {0}</span></li>", dt.Rows[0]["User_TelPhone"]);
                    }
                    if (dt.Rows[0]["User_Email"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>电子邮箱：</span><span title=\"{0}\"  class=\"text\"> {0}</span></li>", dt.Rows[0]["User_Email"]);
                    }
                    if (dt.Rows[0]["User_Contact"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>联系人：</span> <span title=\"{0}\"  class=\"text\"> {0}</span></li>", dt.Rows[0]["User_Contact"]);
                    }
                    if (dt.Rows[0]["User_Url"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>公司网址：</span> <span title=\"{0}\"  class=\"text\"> {0}</span></li>", dt.Rows[0]["User_Url"]);
                    }
                    if (dt.Rows[0]["User_Address"].ToString()!="")
                    {
                        builder.AppendFormat("<li><span>公司地址：</span> <span title=\"{0}\"  class=\"text\"> {0}</span></li>", dt.Rows[0]["User_Address"]);
                    }
                    jsonResult.Add("msg",builder.ToString());
                }
                else
                {
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", "0");
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
